<?php
namespace SimplePhp\Tool;

/**
 *默认适用文件
 * 只支持一维数组
 */
class FileNameOtss implements IObjectToSplitStr {
	private $object, $str;
	private $key = '_';
	public function setSplit($key) {
		if ($key == '.') {
			$key = '\.';
		}
		$this->key = $key;
		return $this;
	}
	public function setObject($Object) {
		$this->object = $Object;
		$this->str = "";
		if (!is_array($Object)) {
			$Object = get_object_vars($object);
		}
		foreach ($Object as $key => $value) {
			$this->str .= $key . $this->key . $value . $this->key;
		}
		$this->str = substr($this->str, 0, -1);
		return $this;
	}
	/**
	 * 吧字符串装换成数组或者object对象
	 * @param [type]  $str      [要转换的字符串]
	 * @param boolean $is_array [是否返回数组]
	 */
	public function setStr($str, $is_array = false) {
		$this->str = $str;
		$object = split($this->key, $str);
		$this->object = (object) [];
		for ($i = 0; $i < count($object);) {
			$key = $object[$i];
			$value = $object[$i + 1];
			$this->object->$key = $value;
			$i = $i + 2;
		}
		if ($is_array) {
			$this->object = get_object_vars($this->object);
		}
		return $this;
	}
	public function getStr() {
		return $this->str;
	}
	public function getObject() {
		return $this->object;
	}
}